package com.remoteJavaService;

import org.ksoap2.*;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import com.exiassassin.librairies.game.Game;
import com.exiassassin.librairies.game.Player;
import com.model.PlayerImp;
import android.util.Log;
public class CallGameService extends Game{
	private String IP_ADRESS = "192.168.200.50:8080";
	private static final String NAMESPACE = "http://server.exiassassin.com";
	private String URL = "http://"+getIP_ADRESS()+"/axis2/services/ExiaService?wsdl";
	private static final String METHOD_NAME = "setPlayer";
	private String SOAP_ACTION = "http://"+getIP_ADRESS()+"/axis2/services/ExiaService/setPlayer";
	
	@Override
	public Player connection(PlayerImp player) {
		try {
			Log.i("CallGameService"," connection - en cours");
			SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
			
			/* Preparing request */
			PropertyInfo pi = new PropertyInfo();
				pi.setName("Player");
				pi.setValue(player);
				pi.setType(Player.class);
				pi.namespace = "http://game.librairies.exiassassin.com/xsd";
			request.addProperty(pi);
			Log.i("CallGameService"," PropertyInfo - ok");
			
			/* Preparing envelope */
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);	
				envelope.setAddAdornments(true);
				envelope.setOutputSoapObject(request);
				envelope.dotNet = false;
				envelope.implicitTypes=true;
				envelope.addMapping("http://game.librairies.exiassassin.com/xsd", "Player",Player.class);  
			
			/* Calling service */
			HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 
			//androidHttpTransport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
				androidHttpTransport.debug = true;
				androidHttpTransport.call(SOAP_ACTION, envelope);
			Log.i("CallGameService"," androidHttpTransport - ok : "+androidHttpTransport.requestDump);// Sending debug XML msg
			Log.i("CallGameService","call - ok" +androidHttpTransport.responseDump ); // Receiving debug XML msg
			
			/* Getting response */
			SoapObject response = (SoapObject)envelope.getResponse();
			
			Log.i("CallGameService","response - ok");
			Log.i("CallGameService","response - value"+response.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	@Override
	public Player gameWin() {
		try {
			SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
			//request.addProperty("player", player);
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);		      
			envelope.setOutputSoapObject(request);
			envelope.dotNet = false;
			envelope.addMapping(NAMESPACE, "player",Player.class);

			//  PropertyInfo pi = new PropertyInfo();
			//   pi.setName("player");
			//    pi.setValue(player);
			//  pi.setType(player.getClass());
			//  request.addProperty(pi);

			HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 
			androidHttpTransport.call(SOAP_ACTION, envelope);

			Log.i("CallGameService","call gameWin - ok");	      
			SoapObject response = (SoapObject)envelope.getResponse();

			Log.i("CallGameService","login - ok"+response.toString());
		} catch (Exception e) {
			Log.e("connection", e.toString());
		}
		return null;
	}
	@Override
	public boolean isAlive(Player arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	private String getIP_ADRESS() {
		return IP_ADRESS;
	}

	public void setIP_ADRESS(String iP_ADRESS) {
		IP_ADRESS = iP_ADRESS;
	}




}
